#  -*- coding = utf-8 -*-
# @Time :  22:13
# @Author: blackshark
#@File :back-end.py
#@SoftWare : PyCharm
import flask
import json
import pymysql
from flask import request, jsonify

server=flask.Flask(__name__)

def my_db(sql):
    conn=pymysql.connect(
        host='120.78.179.141',
        user='newsForCrawler',
        password='imtired',
        database='newsforcrawler',
        charset='utf8',
        autocommit=True# 自动提交
    )
    cur = conn.cursor(cursor=pymysql.cursors.DictCursor)  # 建立游标；默认返回二维数组，DictCursor指定返回字典；
    cur.execute(sql)  # execute帮你执行sql
    res1 = cur.fetchall()  # 拿到全部sql执行结果
    cur.close()  # 关闭游标
    conn.close()  # 关闭数据库
    return res1  # 返回sql执行的结果

def request_parse(req_data):
    if req_data.method == 'POST':
        data = req_data.json
    elif req_data.method == 'GET':
        data = req_data.args
    return data

@server.route('/data',methods=['get'])
def returnData():
    data = request_parse(request)
    pageID=data.get("pageID")
    newsID=data.get("newsID")
    res={}
    if pageID and newsID:# 必填参数校
       if not pageID.isdigit():# 如果不是整数类型
           res = {"error_code": 3004, "msg": "页面ID输入错误"}
       elif not newsID.isdigit():# 如果不是整数类型
           res = {"error_code": 3004, "msg": "新闻ID输入错误"}
       else:
           if(int(pageID)==1):
               sql = "select * from jwc where id='%d';" % int(newsID)
               res = {
                       "code":200,
                       "msg":"请求成功",
                       "data":my_db(sql) } # 执行sql
           elif( int(pageID) == 2):
               sql = "select * from cyberSecurity where id='%d';" % int(newsID)
               res = {
                   "code" : 200,
                   "msg" : "请求成功",
                   "data" : my_db(sql)}  # 执行sql
           elif (int(pageID) == 3) :
               sql = "select * from education where id='%d';" % int(newsID)
               res = {
                   "code" : 200,
                   "msg" : "请求成功",
                   "data" : my_db(sql)}  # 执行sql
           elif (int(pageID) == 4) :
               sql = "select * from partyCommittee where id='%d';" % int(newsID)
               res = {
                   "code" : 200,
                   "msg" : "请求成功",
                   "data" : my_db(sql)}  # 执行sql
           elif (int(pageID) == 5) :
               sql = "select * from socialwork where id='%d';" % int(newsID)
               res = {
                   "code" : 200,
                   "msg" : "请求成功",
                   "data" : my_db(sql)}  # 执行sql
           elif (int(pageID) == 6) :
               sql="select * from library where id='%d';"%int(newsID)
               res = {
                   "code" : 200,
                   "msg" : "请求成功",
                   "data" : my_db(sql)}  # 执行sql
    else:
            res = {"error_code": 3007, "msg": "未传必要参数"}

    return  json.dumps(res,ensure_ascii=False)#防止出现乱码

@server.route('/page',methods=['get'])
def returnData2():
    data = request_parse(request)
    pageID=data.get("pageID")
    res={}
    if pageID:# 必填参数校
       if not pageID.isdigit():# 如果不是整数类型
           res = {"error_code": 3004, "msg": "页面ID输入错误"}
       else:
           if(int(pageID)==1):
               sql = "select id,title,information from jwc;"
               res = {
                       "code":200,
                       "msg":"请求成功",
                       "total":30,
                       "data":my_db(sql) } # 执行sql
           elif( int(pageID) == 2):
               sql = "select id,title,information from cyberSecurity;"
               res = {
                   "code" : 200,
                   "msg" : "请求成功",
                   "total":30,
                   "data" : my_db(sql)}  # 执行sql
           elif (int(pageID) == 3) :
               sql = "select id,title,information from education ;"
               res = {
                   "code" : 200,
                   "msg" : "请求成功",
                   "total":30,
                   "data" : my_db(sql)}  # 执行sql
           elif (int(pageID) == 4) :
               sql = "select id,title,information from partyCommittee;"
               res = {
                   "code" : 200,
                   "msg" : "请求成功",
                   "total":30,
                   "data" : my_db(sql)}  # 执行sql
           elif (int(pageID) == 5) :
               sql = "select id,title,information from socialwork;"
               res = {
                   "code" : 200,
                   "msg" : "请求成功",
                   "total":30,
                   "data" : my_db(sql)}  # 执行sql
           elif (int(pageID) == 6) :
               sql="select id,title,information from library;"
               res = {
                   "code" : 200,
                   "msg" : "请求成功",
                   "total":30,
                   "data" : my_db(sql)}  # 执行sql
    else:
            res = {"error_code": 3007, "msg": "未传必要参数"}

    return  json.dumps(res,ensure_ascii=False)#防止出现乱码
server.run(host="0.0.0.0",port=9876,debug=True)
